import sys
from PyQt5.QtCore import Qt, QTimer
from PyQt5.QtWidgets import QPushButton, QInputDialog, QLabel, QLineEdit, QApplication, QMainWindow, QTreeWidget, QHBoxLayout, QVBoxLayout, QWidget, QTextEdit, QMenu, QAction
from snmpProcess import *
from MIB_Tree import MIBTreeGen
from table import DeviceDetail

DEVICE_IP = '192.168.88.100'
COMMUNITY_DATA_READ = 'Public123'
COMMUNITY_DATA_WRITE = 'Private123'

class TreeWidgetExample(QMainWindow):
    def __init__(self):
        super(TreeWidgetExample, self).__init__()
        
        central_widget = QWidget(self)
        self.setCentralWidget(central_widget)
        left_widget = QWidget()
        right_widget = QWidget()
        self.device_detail = DeviceDetail()  # 设备详情
        up_widget = QWidget()  # 上两个组件

        main_layout = QHBoxLayout(central_widget)
        left_layout = QVBoxLayout(left_widget)
        right_layout = QVBoxLayout(right_widget)

        label_ip = QLabel('IP地址：')
        self.device_ip = QLineEdit()
        self.device_ip.setText(DEVICE_IP)
        label_read = QLabel('读团体码：')
        self.read_code = QLineEdit()
        self.read_code.setText(COMMUNITY_DATA_READ)
        label_write = QLabel('写团体码：')
        self.write_code = QLineEdit()
        self.write_code.setText(COMMUNITY_DATA_WRITE)
        self.trap_label = QLabel()
        self.watch_btn = QPushButton('添加到监视')
        self.watch_btn.clicked.connect(self.addWatchAction)
        self.refresh_btn = QPushButton('刷新')
        self.refresh_btn.clicked.connect(self.refreshRightNow)
        self.refresh_timer_lbl = QLabel()
        self.timer = QTimer(self)
        self.timer.timeout.connect(self.updateTimer)
        self.timer.start(1000)
        self.remaining_time = 1

        # 添加树状列表项
        tree_widget = QTreeWidget()
        tree_widget.setHeaderHidden(True)  # 隐藏列标题
        MIB_Tree = MIBTreeGen(tree_widget)

        # 为树状列表项添加右键菜单
        tree_widget.setContextMenuPolicy(Qt.CustomContextMenu)
        tree_widget.customContextMenuRequested.connect(self.showContextMenu)

        # 将 QLabel 和 QLineEdit 添加到布局中
        left_layout.addWidget(label_ip)
        left_layout.addWidget(self.device_ip)
        left_layout.addWidget(label_read)
        left_layout.addWidget(self.read_code)
        left_layout.addWidget(label_write)
        left_layout.addWidget(self.write_code)
        left_layout.addWidget(self.watch_btn)
        left_layout.addWidget(tree_widget)  # MIB
        right_layout.addWidget(self.refresh_btn)
        right_layout.addWidget(self.refresh_timer_lbl)
        right_layout.addWidget(self.device_detail)
        main_layout.addWidget(left_widget)
        main_layout.addWidget(right_widget)
        

        # 创建一个QTextEdit实例用于显示结果
        self.result_text_edit = QTextEdit(self)

        # 将结果显示框添加到布局中
        left_layout.addWidget(self.result_text_edit)
        
        left_layout.addWidget(self.trap_label)

        # 设置主窗口属性
        self.setGeometry(400, 150, 1200, 800)
        self.setWindowTitle('SnmpC')

    def refreshRightNow(self):
        self.device_detail.data_loader_thread.start()
        self.remaining_time = 30
        
    def updateTimer(self):
        self.remaining_time -= 1
        self.refresh_timer_lbl.setText(f'下次刷新：{self.remaining_time}秒')

        if self.remaining_time == 0:
            self.device_detail.data_loader_thread.start()
            self.remaining_time = 30  # 重置倒计时

    def showContextMenu(self, pos):
        # 获取选中的树状列表项
        item = self.sender().itemAt(pos)
        if item is not None:
            # 创建右键菜单
            context_menu = QMenu(self)

            # 添加操作
            action1 = QAction('Get', self)
            action1.triggered.connect(lambda: self.getAction(item))

            action2 = QAction('Get Next', self)
            action2.triggered.connect(lambda: self.getNextAction(item))
            
            action3 = QAction('Walk', self)
            action3.triggered.connect(lambda: self.walkAction(item))
            
            action4 = QAction('Set', self)
            action4.triggered.connect(lambda: self.setAction(item))

            # 将操作添加到菜单
            context_menu.addAction(action1)
            context_menu.addAction(action2)
            context_menu.addAction(action3)
            context_menu.addAction(action4)

            # 显示右键菜单
            context_menu.exec_(self.sender().mapToGlobal(pos))

    def getAction(self, item):
        sp = SnmpProcess(self.device_ip.text(), self.read_code.text(), self.write_code.text())
        self.result_text_edit.setText(sp.commandParserGet(item.data(0, Qt.UserRole)))

    def getNextAction(self, item):
        sp = SnmpProcess(self.device_ip.text(), self.read_code.text(), self.write_code.text())
        self.result_text_edit.setText(sp.commandParserGetNext(item.data(0, Qt.UserRole)))
        
    def walkAction(self, item):
        sp = SnmpProcess(self.device_ip.text(), self.read_code.text(), self.write_code.text())
        self.result_text_edit.setText(sp.commandParserWalk(item.data(0, Qt.UserRole)))
        
    def setAction(self, item):
        sp = SnmpProcess(self.device_ip.text(), self.read_code.text(), self.write_code.text())
        text, ok_pressed = QInputDialog.getText(self, 'Set', '输入值')
        if ok_pressed:
            self.result_text_edit.setText(sp.commandParserSet(item.data(0, Qt.UserRole), text))
    
    def addWatchAction(self):
        self.device_detail.addWatch(self.device_ip.text(), self.read_code.text(), self.write_code.text())

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = TreeWidgetExample()
    window.show()
    
    sys.exit(app.exec_())
